const Service = require('egg').Service;
const SmsCore = require('@alicloud/pop-core');

const client = new SmsCore({
    accessKeyId: 'LTAIIsvXc1QZjWxm',
    accessKeySecret: 'tEez1mFKtsErmFz5UBJv2XrVqeK6w4',
    endpoint: 'https://dysmsapi.aliyuncs.com',
    apiVersion: '2017-05-25'
});

const requestOption = {
    method: 'POST'
};
class SmsService extends Service {
    /**
     * @param {提交的数据} data 
     * @return 1，0
     */
    async sendCode(phone, code) {
        const { app } = this;
        var params = {
            "RegionId": "cn-hangzhou",
            "TemplateCode": "SMS_158440394",
            "TemplateParam": JSON.stringify({
                "code": code
            }),
            "SignName": "九五三",
            "PhoneNumbers": phone
        }
        try {
            let rs = await client.request('SendSms', params, requestOption);
        } catch (error) {
            console.log(error);
            await app.utils.error.throw(error.data.Message, -99);
        }
    }


}

module.exports = SmsService;